// sub_renyuanguanli/renyuanlist/renyuanlist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    parentBankname: "",
    bankindex: "",
    bankInfo: {},
    username: "",
    userId: "",
    searchValue: "",
    isfirstAdmin: false,
    isManage: false,
    list: [],
    pageNum: 1,
    pageSize: 10,
    selectCheckbox: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);

    if (options.username) {
      this.setData({
        username: options.username,
        userId: options.userId
      })
    }
    if (options.id) {
      this.setData({
        id: options.id,
        bankindex: options.index,
      })
      this.getList()
    }
    if (options.bankname) {
      this.setData({
        parentBankname: options.bankname
      })
    }
    this.queerBankInfo()

  },
  onSearch(e) {
    console.log(e);
    this.setData({
      searchValue: e.detail,
    })
    this.readygetList()
  },

  selectChange(e) {
    this.setData({
      selectCheckbox: e.detail,
    });
  },
  cellCheckToggle(e) {
    const {
      index
    } = e.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  noop() {},
  // method
  getList() {
    const params = {
      corpName: this.data.searchValue,
      userId: this.data.userId,
      bankId: this.data.id,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    console.log(params, 'params');
    getApp().globalData.request(
      "/corpVisit/getCorpPage", params,
      "POST"
    ).then(res => {
      console.log(res);
      this.setData({
        list: this.data.list.concat(res.data.list)
      })
    })
  },
  // 判断是否是一级管理员
  isFiradmin() {
    let arr = wx.getStorageSync("userInfo").corpRole;
    // let result = arr.find((item,index)=>{
    //   return item == "fir_admin"
    // })
    if (arr.indexOf("fir_admin") !== -1) {
      this.setData({
        isfirstAdmin: true,
      })
    } else {
      this.setData({
        isfirstAdmin: false,
      })
    }
  },
  // 查询银行信息
  // /query_user_bank_info
  queerBankInfo() {
    getApp().globalData.request(
      "/corpVisit/query_user_bank_info", {
        bankId: this.data.id
      },
      "POST"
    ).then(res => {
      console.log(res);
      if (res.code == 200) {
        this.setData({
          bankInfo: res.data
        })
      }
    })
  },

  scrollBottom () {
    this.setData({
      pageNum: ++this.data.pageNum,
    });
    this.queerBankInfo();
  },

  ischeckedChange(e) {
    console.log(e);
    if (e.detail) {
      this.data.list.forEach((item, index) => {
        this.data.selectCheckbox.push(item.corpCredit)
      })
      this.setData({
        selectCheckbox: this.data.selectCheckbox
      })
    } else {
      this.setData({
        selectCheckbox: []
      })
    }
    this.setData({
      ischecked: e.detail,
    });
  },

  fenpeiClick(e) {
    // if (this.data.selectCheckbox.length == 0) {
    //   wx.showToast({
    //     title: '清先选择您要分配的企业！',
    //     icon: "none"
    //   })
    // } else {
    //   wx.navigateTo({
    //     url: `/sub_allotPro/module2/page4/page4?organizationIds=${ JSON.stringify(this.data.selectCheckbox)}&bankId=${this.data.id}`,
    //   })
    // }
    wx.navigateTo({
      url: `/sub_allotPro/module2/page4/page4?organizationIds=${ JSON.stringify(this.data.selectCheckbox)}&bankId=${this.data.id}&type=2&listId=${this.data.bankInfo.branchBankId}`,
    })
  },
  readygetList() {
    this.setData({
      pageNum: "1",
      list: [],
      selectCheckbox: []
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  scrollBottom() {
    this.setData({
      pageNum: this.data.pageNum * 1 + 1,
    })
    // console.log(this.data.pageNum);
    this.getList()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.refresh) {
      this.readygetList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})